package com.crazy.controller.sys;

import static com.crazy.common.page.Pager.DEFAULT_COUNT;
import static org.hibernate.criterion.Order.desc;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crazy.entity.sys.PubSysLog;
import com.crazy.service.sys.PubSysLogService;

/**
 * 系统日志控制器
 * 
 * @author admin
 */
@Controller
@RequestMapping("/sys/log")
public class PubSysLogController {
	@Inject
	private PubSysLogService service;

	@RequestMapping("/list")
	public String list(Integer pageNo,
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
		Page<PubSysLog> page = this.service.findSysLogs(begin, end, pageNo,
				DEFAULT_COUNT, desc("accessTime"));
		model.addAttribute("page", page);
		model.addAttribute("begin", begin);
		model.addAttribute("end", end);
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String now = format.format(new Date());
		model.addAttribute("now", now);
		return "sys/log/list";
	}

	@RequestMapping("/query")
	public String query(Model model) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String now = format.format(new Date());
		model.addAttribute("now", now);
		return "sys/log/query";
	}

	@RequestMapping("/doQuery")
	@ResponseBody
	public Object doQuery(String begin, String end) {
		List<Object[]> list = this.service.query(begin, end);
		return list;
	}
}
